<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :before-close="cancel">
    <el-form label-width="120px" :model="formData=data?data:formData" :rules="rules" ref="form">
      <el-form-item label="权限名称" prop="name">
        <el-input style="width:90%" v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input style="width:90%" v-model="formData.code"></el-input>
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input style="width:90%" v-model="formData.description"></el-input>
      </el-form-item>
      <el-form-item label="开启">
        <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0"></el-switch>
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="submit">确定</el-button>
          <el-button size="small" @click="cancel">取消</el-button>
        </el-col>
      </el-row>
  </el-dialog>
</template>


<script>
import {addPermissions,updatePermissions} from '@/api/permissions'
export default {
  props:{
    dialogVisible:{
      type:Boolean,
      default:false
    },
    title:{
      default:''
    },
    fatherID:{
      default:null
    },
    data:{
      default:null
    }
  },
  data() {
    return {
      formData:{
        name:'',
        code:'',
        description:'',
        enVisible:'',
        type:'',
        pid:''
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      },
    }
  },
  // data
  methods:{
    cancel() {
      this.$refs.form.resetFields()
      this.formData={
        name:'',
        code:'',
        description:'',
        enVisible:'',
        type:'',
        pid:''
      }
      this.$emit('update:dialogVisible',false)
      this.$emit('update:data',null)
    },
    // 确定
    async submit() {
      await this.$refs.form.validate()
      // 创建权限
      if(this.title=='新增权限点') {
        // 创建子权限
        if(this.fatherID) {
          this.formData.type=2
          this.formData.pid=this.fatherID
          this.$emit('update:fatherID',null)
        }
        // 创建根权限
        else {
          this.formData.type=1
          this.formData.pid=0
        }
        await addPermissions(this.formData)
        this.$message({
          message: '添加权限成功',
          type: 'success'
        });
      }
      // 编辑权限
      else {
        await updatePermissions(this.data.id,this.data)
        this.$message({
          message: '编辑权限成功',
          type: 'success'
        });
      }

      // 编辑子权限
      this.$emit('initData')
      this.$emit('update:dialogVisible',false)
    }
  },
  created() {
  },
  computed:{
  }
}
</script>


<style>
</style>